package com.petadoption.serverclient.controller;


import com.petadoption.serverclient.entity.Address;
import com.petadoption.serverclient.entity.User;
import com.petadoption.serverclient.service.IAddressService;
import com.petadoption.serverclient.utils.Result;
import com.petadoption.serverclient.utils.TokenUtil;
import com.petadoption.serverclient.vo.AddressVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private Result result;
    @Resource
    private IAddressService addressService;

    @GetMapping("/list")
    public Result findAddressAll() {
        User user = TokenUtil.getCurrenUser();
        List<AddressVO> list = addressService.findAddressAll(user.getUserId());
        if (list.isEmpty()) return result.fail("暂无地址");
        return result.success("地址列表", list);
    }

    @PostMapping("/add")
    public Result AddAddress(@RequestBody AddressVO addressVO) {
        User user = TokenUtil.getCurrenUser();
        Address address = new Address();
        address.setAddressProvince(addressVO.getProvince());
        address.setAddressCity(addressVO.getCity());
        address.setAddressCounty(addressVO.getCounty());
        address.setAddressDetail(addressVO.getAddressDetail());
        address.setAddressCode(addressVO.getAreaCode());
        address.setAddressIsdefault(addressVO.getIsDefault());
        address.setAddressFid(addressVO.getAddressFid());
        addressVO.setAddressUid(user.getUserId());
        address.setAddressTel(addressVO.getTel());
        address.setAddressName(address.getAddressName());
        int i = addressService.addAddress(addressVO);
        if (i > 0) return result.success("新增成功");
        return result.fail("新增地址失败");
    }

    @PostMapping("/edit")
    public Result updateAddress(@RequestBody AddressVO addressVO) {
        User user = TokenUtil.getCurrenUser();
        Address address = new Address();
        address.setAddressId(addressVO.getAddressId());
        address.setAddressProvince(addressVO.getProvince());
        address.setAddressCity(addressVO.getCity());
        address.setAddressCounty(addressVO.getCounty());
        address.setAddressDetail(addressVO.getAddressDetail());
        address.setAddressCode(addressVO.getAreaCode());
        address.setAddressIsdefault(addressVO.getIsDefault());
        address.setAddressFid(addressVO.getAddressFid());
        address.setAddressUid(user.getUserId());
        address.setAddressTel(addressVO.getTel());
        address.setAddressName(addressVO.getName());
        int i = addressService.updateAddress(address);
        if (i > 0) return result.success("修改成功");
        return result.fail("修改失败");
    }

    @GetMapping("/{addressId}")
    public Result updateAddress(@PathVariable int addressId) {
        User user = TokenUtil.getCurrenUser();
        Address address = addressService.findAddress(addressId, user.getUserId());
        AddressVO addressVO = new AddressVO();
        addressVO.setAddressId(address.getAddressId());
        addressVO.setProvince(address.getAddressProvince());
        addressVO.setCity(address.getAddressCity());
        addressVO.setCounty(address.getAddressCounty());
        addressVO.setAddressDetail(address.getAddressDetail());
        addressVO.setAreaCode(address.getAddressCode());
        addressVO.setIsDefault(address.getAddressIsdefault());
        addressVO.setAddressFid(address.getAddressFid());
        addressVO.setAddressUid(user.getUserId());
        addressVO.setTel(address.getAddressTel());
        addressVO.setName(address.getAddressName());
        if (address == null) return result.fail("查询失败");
        return result.success("查询成功", addressVO);
    }

    @GetMapping("/delete/{addressId}")
    public Result deleteAddress(@PathVariable int addressId) {
        User user = TokenUtil.getCurrenUser();
        int i = addressService.deleteAddress(addressId, user.getUserId());
        if (i > 0) return result.success("删除成功");
        return result.fail("删除失败");
    }

}
